<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>coordinates.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_graphics_ext_coordinates.js.html">coordinates.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>
<a name="line16"></a>/**
<a name="line17"></a> * @fileoverview Graphics utility functions for advanced coordinates.
<a name="line18"></a> *
<a name="line19"></a> * This file assists the use of advanced coordinates in goog.graphics.  Coords
<a name="line20"></a> * can be specified as simple numbers which will correspond to units in the
<a name="line21"></a> * graphics element&#39;s coordinate space.  Alternately, coords can be expressed
<a name="line22"></a> * in pixels, meaning no matter what tranformations or coordinate system changes
<a name="line23"></a> * are present, the number of pixel changes will remain constant.  Coords can
<a name="line24"></a> * also be expressed as percentages of their parent&#39;s size.
<a name="line25"></a> *
<a name="line26"></a> * This file also allows for elements to have margins, expressable in any of
<a name="line27"></a> * the ways described above.
<a name="line28"></a> *
<a name="line29"></a> * Additional pieces of advanced coordinate functionality can (soon) be found in
<a name="line30"></a> * element.js and groupelement.js.
<a name="line31"></a> *
<a name="line32"></a> * @author robbyw@google.com (Robby Walker)
<a name="line33"></a> */
<a name="line34"></a>
<a name="line35"></a>goog.provide(&#39;goog.graphics.ext.coordinates&#39;);
<a name="line36"></a>
<a name="line37"></a>goog.require(&#39;goog.string&#39;);
<a name="line38"></a>
<a name="line39"></a>
<a name="line40"></a>/**
<a name="line41"></a> * Cache of boolean values.  For a given string (key), is it special? (value)
<a name="line42"></a> * @type {Object}
<a name="line43"></a> * @private
<a name="line44"></a> */
<a name="line45"></a>goog.graphics.ext.coordinates.specialCoordinateCache_ = {};
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>/**
<a name="line49"></a> * Determines if the given coordinate is a percent based coordinate or an
<a name="line50"></a> * expression with a percent based component.
<a name="line51"></a> * @param {string} coord The coordinate to test.
<a name="line52"></a> * @return {boolean} Whether the coordinate contains the string &#39;%&#39;.
<a name="line53"></a> * @private
<a name="line54"></a> */
<a name="line55"></a>goog.graphics.ext.coordinates.isPercent_ = function(coord) {
<a name="line56"></a>  return goog.string.contains(coord, &#39;%&#39;);
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Determines if the given coordinate is a pixel based coordinate or an
<a name="line62"></a> * expression with a pixel based component.
<a name="line63"></a> * @param {string} coord The coordinate to test.
<a name="line64"></a> * @return {boolean} Whether the coordinate contains the string &#39;px&#39;.
<a name="line65"></a> * @private
<a name="line66"></a> */
<a name="line67"></a>goog.graphics.ext.coordinates.isPixels_ = function(coord) {
<a name="line68"></a>  return goog.string.contains(coord, &#39;px&#39;);
<a name="line69"></a>};
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Determines if the given coordinate is special - i.e. not just a number.
<a name="line74"></a> * @param {string|number|null} coord The coordinate to test.
<a name="line75"></a> * @return {boolean} Whether the coordinate is special.
<a name="line76"></a> */
<a name="line77"></a>goog.graphics.ext.coordinates.isSpecial = function(coord) {
<a name="line78"></a>  var cache = goog.graphics.ext.coordinates.specialCoordinateCache_;
<a name="line79"></a>
<a name="line80"></a>  if (!(coord in cache)) {
<a name="line81"></a>    cache[coord] = goog.isString(coord) &amp;&amp; (
<a name="line82"></a>        goog.graphics.ext.coordinates.isPercent_(coord) ||
<a name="line83"></a>        goog.graphics.ext.coordinates.isPixels_(coord));
<a name="line84"></a>  }
<a name="line85"></a>
<a name="line86"></a>  return cache[coord];
<a name="line87"></a>};
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * Returns the value of the given expression in the given context.
<a name="line92"></a> *
<a name="line93"></a> * Should be treated as package scope.
<a name="line94"></a> *
<a name="line95"></a> * @param {string|number} coord The coordinate to convert.
<a name="line96"></a> * @param {number} size The size of the parent element.
<a name="line97"></a> * @param {number} scale The ratio of pixels to units.
<a name="line98"></a> * @return {number} The number of coordinate space units that corresponds to
<a name="line99"></a> *     this coordinate.
<a name="line100"></a> */
<a name="line101"></a>goog.graphics.ext.coordinates.computeValue = function(coord, size, scale) {
<a name="line102"></a>  var number = parseFloat(String(coord));
<a name="line103"></a>  if (goog.isString(coord)) {
<a name="line104"></a>    if (goog.graphics.ext.coordinates.isPercent_(coord)) {
<a name="line105"></a>      return number * size / 100;
<a name="line106"></a>    } else if (goog.graphics.ext.coordinates.isPixels_(coord)) {
<a name="line107"></a>      return number / scale;
<a name="line108"></a>    }
<a name="line109"></a>  }
<a name="line110"></a>
<a name="line111"></a>  return number;
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Converts the given coordinate to a number value in units.
<a name="line117"></a> *
<a name="line118"></a> * Should be treated as package scope.
<a name="line119"></a> *
<a name="line120"></a> * @param {string|number} coord The coordinate to retrieve the value for.
<a name="line121"></a> * @param {boolean|undefined} forMaximum Whether we are computing the largest
<a name="line122"></a> *     value this coordinate would be in a parent of no size.  The container
<a name="line123"></a> *     size in this case should be set to the size of the current element.
<a name="line124"></a> * @param {number} containerSize The unit value of the size of the container of
<a name="line125"></a> *     this element.  Should be set to the minimum width of this element if
<a name="line126"></a> *     forMaximum is true.
<a name="line127"></a> * @param {number} scale The ratio of pixels to units.
<a name="line128"></a> * @param {Object=} opt_cache Optional (but highly recommend) object to store
<a name="line129"></a> *     cached computations in.  The calling class should manage clearing out
<a name="line130"></a> *     the cache when the scale or containerSize changes.
<a name="line131"></a> * @return {number} The correct number of coordinate space units.
<a name="line132"></a> */
<a name="line133"></a>goog.graphics.ext.coordinates.getValue = function(coord, forMaximum,
<a name="line134"></a>    containerSize, scale, opt_cache) {
<a name="line135"></a>  if (!goog.isNumber(coord)) {
<a name="line136"></a>    var cacheString = opt_cache &amp;&amp; ((forMaximum ? &#39;X&#39; : &#39;&#39;) + coord);
<a name="line137"></a>
<a name="line138"></a>    if (opt_cache &amp;&amp; cacheString in opt_cache) {
<a name="line139"></a>      coord = opt_cache[cacheString];
<a name="line140"></a>    } else {
<a name="line141"></a>      if (goog.graphics.ext.coordinates.isSpecial(
<a name="line142"></a>          /** @type {string} */ (coord))) {
<a name="line143"></a>        coord = goog.graphics.ext.coordinates.computeValue(coord,
<a name="line144"></a>            containerSize, scale);
<a name="line145"></a>      } else {
<a name="line146"></a>        // Simple coordinates just need to be converted from a string to a
<a name="line147"></a>        // number.
<a name="line148"></a>        coord = parseFloat(/** @type {string} */ (coord));
<a name="line149"></a>      }
<a name="line150"></a>
<a name="line151"></a>      // Cache the result.
<a name="line152"></a>      if (opt_cache) {
<a name="line153"></a>        opt_cache[cacheString] = coord;
<a name="line154"></a>      }
<a name="line155"></a>    }
<a name="line156"></a>  }
<a name="line157"></a>
<a name="line158"></a>  return coord;
<a name="line159"></a>};
</pre>


</body>
</html>
